package com.whmall.service.crm.module.supplierreview.impl;

import cn.hutool.core.util.StrUtil;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.module.supplierreview.SupplierReviewQueryCriteria;
import com.whmall.service.crm.module.supplierreview.SupplierReviewRepository;
import com.whmall.service.crm.module.supplierreview.entity.SupplierReviewAttachmentDO;
import com.whmall.service.crm.module.supplierreview.entity.SupplierReviewDO;
import com.whmall.service.crm.module.supplierreview.entity.SupplierReviewInformationDO;
import com.whmall.service.crm.module.supplierreview.entity.SupplierReviewTagStatisticsDO;
import com.whmall.service.crm.module.supplierreview.impl.dao.SupplierReviewAttachmentMapper;
import com.whmall.service.crm.module.supplierreview.impl.dao.SupplierReviewInformationMapper;
import com.whmall.service.crm.module.supplierreview.impl.dao.SupplierReviewMapper;
import com.whmall.service.crm.module.supplierreview.impl.dao.SupplierReviewTagStatisticsMapper;
import com.whmall.service.crm.util.PageDTOHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SupplierReviewRepositoryImpl
 *
 * @author alex
 * @since 2021/1/19
 */
@Repository
@RequiredArgsConstructor
public class SupplierReviewRepositoryImpl implements SupplierReviewRepository {
    private final SupplierReviewMapper supplierReviewMapper;
    private final SupplierReviewInformationMapper supplierReviewInformationMapper;
    private final SupplierReviewAttachmentMapper supplierReviewAttachmentMapper;
    private final SupplierReviewTagStatisticsMapper supplierReviewTagStatisticsMapper;

    @Override
    public PageRspDTO<SupplierReviewDO> findReviewPageByQueryCriteria(PageReqDTO pageRequest, SupplierReviewQueryCriteria criteria) {
        PageDTOHelper.startPage(pageRequest);
        if(StrUtil.isNotBlank(criteria.getTagIds())){
            criteria.getTagIds().split(",");
            List<Integer> tagList = Arrays.asList(criteria.getTagIds().split(",")).stream().map(rId -> {
                return Integer.parseInt(rId);
            }).collect(Collectors.toList());
            criteria.setTagList(tagList);
        }
        List<SupplierReviewDO> results = supplierReviewMapper.findByQuertCriteria(criteria);
        return PageDTOHelper.toPageDTO(results);
    }

    @Override
    public List<SupplierReviewDO> findReviewListByQueryCriteria(SupplierReviewQueryCriteria criteria) {
        return supplierReviewMapper.findByQuertCriteria(criteria);
    }

    @Override
    public List<SupplierReviewInformationDO> findInformationByReviewIds(List<Long> reviewIds) {
        if (null == reviewIds || reviewIds.isEmpty()) {
            return Collections.emptyList();
        }

        return supplierReviewInformationMapper.findByIdIn(reviewIds);
    }

    @Override
    public List<SupplierReviewAttachmentDO> findAttachementByReviewIds(List<Long> reviewIds) {
        if (null == reviewIds || reviewIds.isEmpty()) {
            return Collections.emptyList();
        }
        return supplierReviewAttachmentMapper.findByReviewIdIn(reviewIds);
    }

    @Override
    public List<SupplierReviewTagStatisticsDO> findReviewTagStatsByCompanyId(Long companyId) {
        Assert.notNull(companyId, "公司ID不能为空");
        return supplierReviewTagStatisticsMapper.findByCompanyId(companyId);
    }
}
